-- ============================================
-- PLINKO - ComputerCraft / CC:Tweaked
-- Nécessite : Advanced Computer + Advanced Monitor (couleur)
-- ============================================

local MISE = 1
local points = 100

local ROWS = 8           -- nombre de rangees de picots (9 cases d'arrivee)
local COL_SPACING = 4     -- espace horizontal entre deux picots
local ROW_SPACING = 2     -- espace vertical entre deux rangees
local DROP_TICK = 0.25    -- vitesse de chute de la bille

-- Multiplicateurs par case d'arrivee (bords eleves, centre risque)
local PAYOUTS = { 10, 4, 2, 1, 0, 1, 2, 4, 10 }

local function payoutColor(mult)
    if mult >= 10 then return colors.magenta
    elseif mult >= 4 then return colors.orange
    elseif mult >= 2 then return colors.yellow
    elseif mult >= 1 then return colors.lightGray
    else return colors.gray end
end

local mon = peripheral.find("monitor")
if not mon then
    error("Aucun monitor trouve ! Colle un Advanced Monitor a cote du computer.")
end

mon.setTextScale(0.5)
local prevTerm = term.redirect(mon)
local w, h = term.getSize()

local boardCenterX = math.floor(w / 2)
local boardTop = 6

local function centerText(y, text, textColor, bgColor)
    term.setBackgroundColor(bgColor or colors.black)
    term.setTextColor(textColor or colors.white)
    local x = math.floor((w - #text) / 2) + 1
    term.setCursorPos(x, y)
    term.write(text)
end

local function fillArea(x1, y1, x2, y2, bg)
    term.setBackgroundColor(bg)
    for y = y1, y2 do
        term.setCursorPos(x1, y)
        term.write(string.rep(" ", x2 - x1 + 1))
    end
end

local function posX(row, p)
    return boardCenterX + (2 * p - row) * math.floor(COL_SPACING / 2)
end

local function posY(row)
    return boardTop + row * ROW_SPACING
end

-- === DESSIN DU PLATEAU ===
local function drawPegs()
    term.setTextColor(colors.lightGray)
    for row = 1, ROWS - 1 do
        for p = 0, row do
            local x, y = posX(row, p), posY(row)
            term.setBackgroundColor(colors.black)
            term.setCursorPos(x, y)
            term.write(".")
        end
    end
end

local bucketPos = {}

local function drawBuckets()
    local y = posY(ROWS) + 1
    for p = 0, ROWS do
        local x = posX(ROWS, p)
        local x1, x2 = x - 1, x + 1
        local mult = PAYOUTS[p + 1]
        term.setBackgroundColor(payoutColor(mult))
        term.setTextColor(colors.white)
        for xx = x1, x2 do
            term.setCursorPos(xx, y)
            term.write(" ")
        end
        local label = tostring(mult)
        term.setCursorPos(x - math.floor(#label / 2), y)
        term.write(label)
        bucketPos[p] = { x1 = x1, x2 = x2, y = y }
    end
end

local function drawHeader()
    fillArea(1, 1, w, boardTop - 1, colors.black)
    centerText(1, "=== CASINO COMPUTERCRAFT ===", colors.yellow)
    centerText(2, "PLINKO", colors.white)
    centerText(3, "Points : " .. points, colors.lime)
end

local function drawBoard(ballRow, ballP)
    term.setBackgroundColor(colors.black)
    term.clear()
    drawHeader()
    drawPegs()
    drawBuckets()

    if ballRow then
        local x, y = posX(ballRow, ballP), posY(ballRow)
        term.setBackgroundColor(colors.black)
        term.setTextColor(colors.yellow)
        term.setCursorPos(x, y)
        term.write("O")
    end
end

-- === UNE MANCHE ===
local function playRound()
    if points < MISE then
        drawBoard(nil, nil)
        centerText(math.floor(h / 2), "PLUS ASSEZ DE POINTS !", colors.red)
        sleep(2)
        return
    end
    points = points - MISE

    -- genere le chemin de la bille (0 = gauche, 1 = droite a chaque picot)
    local p = 0
    drawBoard(0, 0)
    sleep(DROP_TICK)

    for row = 1, ROWS do
        local decision = math.random(0, 1)
        p = p + decision
        drawBoard(row, p)
        sleep(DROP_TICK)
    end

    local mult = PAYOUTS[p + 1]
    local gain = MISE * mult
    points = points + gain

    -- flash de la case d'arrivee
    local b = bucketPos[p]
    term.setBackgroundColor(colors.white)
    term.setTextColor(colors.black)
    for xx = b.x1, b.x2 do
        term.setCursorPos(xx, b.y)
        term.write(" ")
    end
    local label = tostring(mult)
    term.setCursorPos(b.x1 + 1 - math.floor(#label / 2), b.y)
    term.write(label)

    drawHeader()
    local msg, msgColor
    if gain > 0 then
        msg, msgColor = "Bille tombee en x" .. mult .. " ! +" .. gain, colors.lime
    else
        msg, msgColor = "Bille au centre... perdu !", colors.red
    end
    centerText(h, msg, msgColor)
    sleep(2.5)
end

-- === ECRAN D'ACCUEIL ===
local button = {}

local function drawIdle()
    drawBoard(nil, nil)
    local label = "MISER (" .. MISE .. " point) ET LACHER LA BILLE"
    local bw = #label + 4
    local x1 = math.max(1, math.floor((w - bw) / 2) + 1)
    local y = 4
    term.setBackgroundColor(colors.green)
    term.setTextColor(colors.white)
    for x = x1, math.min(w, x1 + bw - 1) do
        term.setCursorPos(x, y)
        term.write(" ")
    end
    term.setCursorPos(x1 + 2, y)
    term.write(label)
    button = { x1 = x1, x2 = x1 + bw - 1, y = y }
end

-- === PROGRAMME PRINCIPAL ===
drawIdle()

while true do
    local event, side, x, y = os.pullEvent("monitor_touch")
    if x >= button.x1 and x <= button.x2 and y == button.y then
        playRound()
        drawIdle()
    end
end